import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_native_splash/flutter_native_splash.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:geomancy/pages/calendar/index.dart';
import 'package:geomancy/pages/home/index.dart';
import 'package:geomancy/pages/my/index.dart';
import 'package:geomancy/providers/system_provider.dart';
import 'package:geomancy/styles/index.dart';
import 'package:geomancy/tools/auth_login.dart';
import 'package:geomancy/tools/utils.dart';
import 'package:move_to_background/move_to_background.dart';
import 'package:provider/provider.dart';

class AppFramePage extends StatefulWidget {
  const AppFramePage({super.key});

  @override
  State<AppFramePage> createState() => _AppFramePageState();
}

class _AppFramePageState extends State<AppFramePage> {
  int quitNum = 0;//记录按返回键的次数
  final PageController _controller = PageController();
  int _currentTabIndex = 0;
  List<dynamic> bottomTabs = [{
    'label': '',
    'iconUrl': 'assets/icons/icon_tab_bottom_home.png',
    'iconActiveUrl': 'assets/icons/icon_tab_bottom_home_active.png'
  },
  //   {
  //   'label': '',
  //   'iconUrl': 'assets/icons/icon_tab_bottom_calendar.png',
  //   'iconActiveUrl': 'assets/icons/icon_tab_bottom_calendar_active.png'
  // },
    {
    'label': '',
    'iconUrl': 'assets/icons/icon_tab_bottom_my.png',
    'iconActiveUrl': 'assets/icons/icon_tab_bottom_my_active.png'
  }];

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      ///移除闪屏
      FlutterNativeSplash.remove();
      ///初始化阿里云一键登录sdk
      AliAuthLoginTools.init(context);
      context.read<SystemProvider>().setTopSafeHeight = MediaQuery.of(context).padding.top;
    });
  }

  onWillPop(bool didPop) {
    if (Platform.isAndroid) {
      if (quitNum > 0) {
        quitNum = 0;
        MoveToBackground.moveTaskToBack();
      }
      quitNum++;
      EasyLoading.showToast('再滑一次退出');
      Future.delayed(const Duration(seconds: 2), () {
        quitNum = 0;
      });
      return;
    }
  }

  tabClickHandle(int index) {
    if (index == _currentTabIndex) return;
    Utils.shake();
    // if ((index == 3 || index == 4) && !Utils.isLogin(context)) {
    //   return;
    // }
    // Utils.shake();
    setState(() {
      _currentTabIndex = index;
    });
    _controller.jumpToPage(index);
  }

  @override
  Widget build(BuildContext context) {
    return PopScope(
      canPop: Platform.isIOS,
      onPopInvoked: onWillPop,
      child: Scaffold(
        resizeToAvoidBottomInset: false,
        body: PageView(
          controller: _controller,
          physics: const NeverScrollableScrollPhysics(),
          children: const [
            HomePage(),
            // CalendarPage(),
            MyPage(),
          ]
        ),
        bottomNavigationBar: Container(
          padding: EdgeInsets.only(left: 80.w, right: 80.w),
          decoration: BoxDecoration(
            color: ColorStyle.pageDefaultColor,
            border: Border(top: BorderSide(width: 1.w, color: ColorStyle.borderColor))
          ),
          child: SafeArea(
            maintainBottomViewPadding: true,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: bottomTabs.asMap().keys.map((int index) {
                return GestureDetector(
                  onTap: () {
                    tabClickHandle(index);
                  },
                  child: Container(
                    // width: 75.w,
                    height: 120.w,
                    color: Colors.transparent,
                    alignment: Alignment.center,
                    child: Image.asset(_currentTabIndex == index ? bottomTabs[index]['iconActiveUrl'] : bottomTabs[index]['iconUrl'], width: 86.w),
               ),
                );
              }).toList(),
            ),
          ),
        ),
        floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      ),
    );
  }
}
